package com.java.zhuiyun.api.jielongidiomlevel.service.impl;

import com.java.zhuiyun.api.jielongidiomlevel.entity.JieLongIdiomLevel;
import com.java.zhuiyun.api.jielongidiomlevel.mapper.JieLongIdiomLevelMapper;
import com.java.zhuiyun.api.jielongidiomlevel.model.JieLongIdiomLevelModel;
import com.java.zhuiyun.api.jielongidiomlevel.service.JieLongIdiomLevelService;
import com.java.zhuiyun.api.jielongidiomquestion.entity.JieLongIdiomQuestion;
import com.java.zhuiyun.api.jielongidiomquestion.mapper.JieLongIdiomQuestionMapper;
import com.java.zhuiyun.common.errorCode.CommonException;
import com.java.zhuiyun.common.response.CommonResult;
import com.java.zhuiyun.util.BeanUtils;
import com.java.zhuiyun.util.pageUtil.MybatisPageHelper;
import com.java.zhuiyun.util.pageUtil.PageRequest;
import com.java.zhuiyun.util.pageUtil.PageResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ClassName JieLongIdiomLevelServiceImpl
 * @Description TODO 成语接龙关卡业务逻辑层实现类 后台管理查询关卡、添加关卡、更新关卡信息、uni-app查询关卡
 * @Author 杨友朋
 * @Date 2023/9/13 9:16
 **/
@Service
public class JieLongIdiomLevelServiceImpl implements JieLongIdiomLevelService {
    @Resource
    JieLongIdiomLevelMapper jieLongIdiomLevelMapper;

    @Resource
    JieLongIdiomQuestionMapper jieLongIdiomQuestionMapper;

    // 1-10 汉语
    private static final String[] NUMBERS = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
    // 计数单位
    private static final String[] UNITS = {"", "十", "百", "千", "万", "十万", "百万", "千万", "亿", "十亿", "百亿", "千亿"};

    /**
     * @param
     * @param id
     * @Author 杨友朋
     * @Description TODO 查询关卡
     * @Date 2023/9/13 10:39
     * @Return
     */
    @Override
    public PageResult selectLevel(PageRequest pageRequest, Integer id) {

        // 分页条件
        MybatisPageHelper.startPage(pageRequest);
        // 调用持久层接口 查询关卡
        List<JieLongIdiomLevelModel> jieLongIdiomLevels = jieLongIdiomLevelMapper.selectLevel(id);
        // 循环关卡信息
        for (int a=0;a<jieLongIdiomLevels.size();a++){
            // 根据关卡id查询关卡词汇信息
            List<JieLongIdiomQuestion> jieLongIdiomQuestions = jieLongIdiomQuestionMapper.selectQuestion(jieLongIdiomLevels.get(a).getId());
            // 判断信息是否为空
            if(jieLongIdiomQuestions!=null){
                // 循环所有词汇
                for (int b=0;b<jieLongIdiomQuestions.size();b++){
                    // 获取词语
                    String idiomValue = jieLongIdiomQuestions.get(b).getIdiomValue();
                    // 判断词语是否存在
                    if(idiomValue==null){
                        // 不存在跳出循环
                        continue;
                    }
                    // 存在获取关卡表词语信息
                    String idiomValue1 = jieLongIdiomLevels.get(a).getIdiomValue();
                    // 判断关卡表词语是否存在
                    if(idiomValue1!=null){
                        // 存在则将成语拼接
                        String s = jieLongIdiomLevels.get(a).getIdiomValue() + "," + idiomValue;
                        // 拼接后赋值给关卡表词语
                        jieLongIdiomLevels.get(a).setIdiomValue(s);
                    }else {
                        // 不存在则直接赋值
                        jieLongIdiomLevels.get(a).setIdiomValue(idiomValue);
                    }
                }
            }
            // 关卡数
            String chineseNumber = convertToChinese(jieLongIdiomLevels.get(a).getId());
            // 给当前关卡数赋值
            jieLongIdiomLevels.get(a).setLevel("第"+chineseNumber+"关");
        }
        PageResult pageResult = MybatisPageHelper.getPageResult(jieLongIdiomLevels);
        return pageResult;
    }

    /**
     * @Author 杨友朋
     * @Description TODO 添加关卡
     * @Date 2023/9/13 10:55
     * @Return CommonResult
     * @param jieLongIdiomLevel
     */
    @Override
    public CommonResult insertLevel(JieLongIdiomLevelModel jieLongIdiomLevel) throws CommonException {
        if(jieLongIdiomLevel!=null){
            // 创建时间
            jieLongIdiomLevel.setCreateTime(new Date());
            // 修改时间
            jieLongIdiomLevel.setUpdateTime(new Date());
            // 创建实体类对象
            JieLongIdiomLevel j=new JieLongIdiomLevel();
            // 模型赋值给实体类
            BeanUtils.copy(jieLongIdiomLevel,j);
            // 调用持久层接口 存入数据库
            Integer integer = jieLongIdiomLevelMapper.insertLevel(j);
            // 判断操作是否成功
            if(integer>0){
                return CommonResult.ok("添加成功");
            } else {
                return CommonResult.error("添加失败");
            }
        }else {
            return CommonResult.error("参数校验失败");
        }

    }

    /**
     * @Author 杨友朋
     * @Description TODO 更新关卡信息
     * @Date 2023/10/12 10:54
     * @Return CommonResult
     * @param jieLongIdiomLevel
     */
    @Override
    public CommonResult updateLevel(JieLongIdiomLevelModel jieLongIdiomLevel) {
        if(jieLongIdiomLevel!=null){
            // 修改时间
            jieLongIdiomLevel.setUpdateTime(new Date());
            // 创建实体类对象
            JieLongIdiomLevel j=new JieLongIdiomLevel();
            // 模型赋值给实体类
            BeanUtils.copy(jieLongIdiomLevel,j);
            // 调用持久层接口 更新数据库数据
            Integer i=jieLongIdiomLevelMapper.updateLevel(j);
            // 判断操作是否成功
            if(i>0){
                return CommonResult.ok();
            }
            return CommonResult.error("修改失败");
        }
        return CommonResult.error("参数校验失败");
    }

    /**
     * @Author 杨友朋
     * @Description TODO uni-app全查所有关卡
     * @Date 2023/10/12 10:50
     * @Return CommonResult
     * @param
     */
    @Override
    public List<JieLongIdiomLevelModel> userSelectLevel() {
        // 调用持久层接口 查询数据库数据
        List<JieLongIdiomLevelModel> list=jieLongIdiomLevelMapper.userSelectLevel();
        return list;
    }

    /**
    * @Author 杨友朋
    * @Description TODO 将阿拉伯数字转化为中文数字表示
    * @Date 2023/10/12 18:58
    * @Return String
    * @param number
    */
    public static String convertToChinese(int number) {
        if (number == 0) {
            return NUMBERS[0];
        }

        StringBuilder sb = new StringBuilder();
        int unitIndex = 0;

        // 依次处理每一位数字
        while (number > 0) {
            // 取出当前位的数字
            int digit = number % 10;
            // 如果该位不为0，则将该位的数字和对应的单位插入到结果字符串的开头
            if (digit != 0) {
                sb.insert(0, UNITS[unitIndex]);
                sb.insert(0, NUMBERS[digit]);
            }
            // 处理下一位数字
            number /= 10;
            unitIndex++;
        }

        // 返回转换后的结果字符串
        return sb.toString();
    }

}
